(function($,window,document){
    function a(op){
        var _op={
            el:".day",
            callback:function(){},
            btnClick:function(){}
        };
        _op = mui.extend(_op,op);
        var dom=mui(_op.el);//3 个元素
        return new Calendar(dom,_op);

    }
    function Calendar(dom,_op){
        var _this=this;
        _this.is_leap=function(year){
            return (year%100==0?res=(year%400==0?1:0):res=(year%4==0?1:0));
        };
        _this.dayBox=document.getElementById('day-box');
        _this.yearBox=document.getElementById('year-box');
        _this.monthBox=document.getElementById('month-box');
        _this.titleBox=mui('.day-title');
        _this.callback=_op.callback;
        _this.btnClick=_op.btnClick;
        _this.dom=dom;
        _this.date=new Date();
        _this.currMonth=_this.date.getMonth();
        _this.currYear=_this.date.getFullYear();
        _this.currTime=_this.date.getDate();
        _this.currDay=_this.date.getDay();//当前星期几
        _this.totalDay=[31,28+_this.is_leap(_this.currYear),31,30,31,30,31,31,30,31,30,31];
        _this.init();
    }
    Calendar.prototype={
        Constructor:Calendar,
        init:function(){
            this.bindDom(this.currMonth);
            this.bindHandler();
        },
        bindHandler:function(){
            var _this=this;
            _this.dom.on('tap','.nu',function(){
                var time = window.parseInt(this.getAttribute('data-time'));
                var date = this.getAttribute('data-date');
                var currDOM=document.getElementsByClassName('curr');
                for(var i=0;i<currDOM.length;i++){
                    currDOM[i].classList.remove('curr');
                }
                this.classList.add('curr');
                _this.dayBox.innerHTML=time;
                _this.callback(date);
            });
            //左
            _this.titleBox.on('tap','#day-prev',function(){
                _this.currMonth--;
                _this.updateMonth(_this.currMonth);
                _this.bindDom(_this.currMonth);
            });
            _this.titleBox.on('tap','#day-next',function(){
                _this.currMonth++;
                _this.updateMonth(_this.currMonth);
                _this.bindDom(_this.currMonth);
            });
            //补签
            _this.titleBox.on('tap','#sign-btn',function(){
                _this.btnClick();
            });
        },
        bindDom:function(Month){
            var _this=this;
            _this.dayBox.innerHTML=_this.currTime;
            _this.yearBox.innerHTML=_this.currYear;
            _this.monthBox.innerHTML=(_this.currMonth+1)+"月";
            _this.monthBox.setAttribute('data-month',_this.currMonth);
            _this.dom[0].innerHTML=_this.createDOM(Month,0);
        },
        updateMonth:function(currMonth){
            var _this=this;
            if(currMonth<0){
                _this.currYear--;
                _this.currMonth=11;//代表12月
            }else if(currMonth>=12){
                _this.currYear++;
                _this.currMonth=0;//代表0月
            }else{
                _this.currMonth=currMonth;//代表12月
            }
            _this.currDay=[31,28+_this.is_leap(_this.currYear),31,30,31,30,31,31,30,31,30,31][_this.currMonth];
            var curr=document.getElementsByClassName('curr')[0];
            if(curr){curr.classList.remove('curr')}
            _this.dayBox.innerHTML=1;
            _this.yearBox.innerHTML= _this.currYear;
            _this.monthBox.innerHTML=(_this.currMonth+1)+"月";
        },
        createDOM:function(Month,k){
            var _this=this;
            var _Year,_Month,_totalDay,Day,domStr="";
            if((Month+k)<0){
                _Year=_this.currYear-1;
                _Month=12+(k+Month);//代表12月
                _totalDay=[31,28+_this.is_leap(_Year),31,30,31,30,31,31,30,31,30,31][_Month];
            }else if((Month+k)>11){
                _Year=_this.currYear+1;
                _Month=Month+k-12;//代表0月
                _totalDay=[31,28+_this.is_leap(_Year),31,30,31,30,31,31,30,31,30,31][_Month];
            }else{
                _Year=_this.currYear;
                _Month=Month+k;
                _totalDay=[31,28+_this.is_leap(_Year),31,30,31,30,31,31,30,31,30,31][_Month];
            }
            Day=new Date(_Year,_Month,1).getDay();//当月第一天星期几
            //i=0 时 是第一个滑动列表，却是最后一各
            //i=1 时 是第二个滑动列表，表示当前
            //i=2 时 是第三个滑动列表，表示后一天
            domStr += '<div class="main clearfix">';
            domStr += '<div class="colorChange">日</div>';
            domStr += '<div class="colorChange">一</div>';
            domStr += '<div class="colorChange">二</div>';
            domStr += '<div class="colorChange">三</div>';
            domStr += '<div class="colorChange">四</div>';
            domStr += '<div class="colorChange">五</div>';
            domStr += '<div class="colorChange">六</div>';
            for (var j = 0, n = Day; j < n; j++) {
                domStr += '<div><span></span></div>';
            }
            var dateMM = _Year + '-' + ( (_Month + 1) < 10 ? ('0' + (_Month + 1)) : (_Month + 1) );
            for (var i = 1, len = _totalDay + 1; i < len; i++) {
                var dayDate = new Date();
                //循环天数是当天
                if(i===dayDate.getDate()){
                    domStr += ['<div class="nu curr" data-time="', i, '" data-date="', dateMM, '-', (i < 10 ? ('0' + i) : i), '"><span>', i, '</span></div>'].join('');
                }else if(_Year>dayDate.getFullYear()||(_Year===dayDate.getFullYear()&&_Month>dayDate.getMonth())||(_Year===dayDate.getFullYear()&&_Month===dayDate.getMonth()&&i>dayDate.getDate()) ){
                    //灰色日期
                    domStr += ['<div class="nu ban" data-time="', i, '" data-date="', dateMM, '-', (i < 10 ? ('0' + i) : i), '"><span>', i, '</span></div>'].join('');
                }else{
                    domStr += ['<div class="nu" data-time="', i, '" data-date="', dateMM, '-', (i < 10 ? ('0' + i) : i), '"><span>', i, '</span></div>'].join('');
                }
            }
            domStr += '</div>';
            return domStr;
        },
        reload:function(){
            var _this=this;
            _this.date=new Date();
            _this.currMonth=_this.date.getMonth();
            _this.currYear=_this.date.getFullYear();
            _this.currTime=_this.date.getDate();
            _this.currDay=_this.date.getDay();//当前星期几
            _this.totalDay=[31,28+_this.is_leap(_this.currYear),31,30,31,30,31,31,30,31,30,31];
            _this.bindDom(this.currMonth);
        }

    };
    $.fn.Calendar = a;
    $.fn.Calendar.Constructor = a;
})($$,window,document);